package com.ct.controller.shipping;

import com.alibaba.excel.EasyExcel;
import com.ct.application.shipping.model.po.ChuanYuanChuQinSearchPO;
import com.ct.application.shipping.model.vo.ChuanYuanChuQinVO;
import com.ct.application.shipping.service.ChuanYuanChuQinService;
import com.ct.commons.BaseController;
import com.ct.commons.annotation.PermissionApi;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 船员出勤
 *
 * @author Auto Generator
 * @since 2023-08-16
 */
@RestController
@PermissionApi(title = "船员出勤", path = "/api/v1/chuanyuanchuqin", code = "AA1000", sorted = 21)
public class ChuanYuanChuQinController extends BaseController {

    @Resource
    private ChuanYuanChuQinService chuanYuanChuQinService;

    /**
     * 查询船员出勤列表
     */
    @PostMapping("/chuanyuanchuqin/list")
    @PermissionApi(title = "查询船员出勤列表", path = "/api/v1/chuanyuanchuqin/list", code = "AA1001", sorted = 1)
    public List<ChuanYuanChuQinVO> findChuanyuanchuqinList(@RequestBody ChuanYuanChuQinSearchPO searchPO, HttpServletResponse response) {
        return chuanYuanChuQinService.findChuanyuanchuqinLists(searchPO);
    }


    /**
     * 导出船员出勤列表
     */
    @PostMapping("/chuanyuanchuqin/export")
    @PermissionApi(title = "导出船员出勤列表", path = "/api/v1/chuanyuanchuqin/export", code = "AA1002", sorted = 2)
    public void export(@RequestBody ChuanYuanChuQinSearchPO searchPO, HttpServletResponse response) {
        List<ChuanYuanChuQinVO> chuanyuanchuqinList = chuanYuanChuQinService.findChuanyuanchuqinLists(searchPO);
        if (chuanyuanchuqinList.size() == 0) {
            throw new RuntimeException("暂无数据可以导出");
        }

        List<List<String>> dataExlVos = new ArrayList<>();
        for (ChuanYuanChuQinVO chuanyuanchuqin : chuanyuanchuqinList) {
            String chuanYuanMing = chuanyuanchuqin.getChuanYuanMing();
            String chuanYuanShenFenHao = chuanyuanchuqin.getChuanYuanShenFenHao();
            String chuanBoMing = chuanyuanchuqin.getChuanBoMing();
            String zhiWei = chuanyuanchuqin.getZhiWei();
            String renZhiShuoMing = chuanyuanchuqin.getRenZhiShuoMing();
            String shangXiaChuanRiQi = chuanyuanchuqin.getShangXiaChuanRiQi();
            String zaiChuanTianShu = chuanyuanchuqin.getZaiChuanTianShu();
            String zhuangTai = chuanyuanchuqin.getZhuangTai();
            String heJiTianShu = chuanyuanchuqin.getHeJiTianShu();
            Map<String, String> monthDays = chuanyuanchuqin.getMonthDays();

            List<String> data = new ArrayList<>();
            data.add(chuanYuanMing);
            data.add(chuanYuanShenFenHao);
            data.add(chuanBoMing);
            data.add(zhiWei);
            data.add(renZhiShuoMing);
            data.add(shangXiaChuanRiQi);
            data.add(zaiChuanTianShu);
            data.add(zhuangTai);
            data.add(heJiTianShu);
            for (String value : monthDays.values()) {
                data.add(value);
            }
            dataExlVos.add(data);
        }

        try (ServletOutputStream outputStream = response.getOutputStream()) {
            // 设置响应头
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("船员出勤", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            EasyExcel.write(outputStream)
                    .head(head(chuanyuanchuqinList))
                    .registerWriteHandler(new CellStyle())
                    .sheet("sheet")
                    .doWrite(dataExlVos);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
    private List<List<String>> head(List<ChuanYuanChuQinVO> chuanyuanchuqinLists) {
        List<String> searchMonths = chuanyuanchuqinLists.get(0).getSearchMonths();
        List<List<String>> list = new ArrayList<List<String>>();
        list.add(List.of("船员姓名"));
        list.add(List.of("船员身份ID"));
        list.add(List.of("船舶名称"));
        list.add(List.of("职位"));
        list.add(List.of("任职说明"));
        list.add(List.of("上下船日期"));
        list.add(List.of("在船天数"));
        list.add(List.of("在船状态"));
        list.add(List.of("合计天数"));
        for (String searchMonth : searchMonths) {
            list.add(List.of(searchMonth));
        }
        return list;
    }


}
